package com.github.alenfive.rocketapi.extendcommon.vo;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.alenfive.rocketapi.extendcommon.error.BusinessErrorEnum;
import lombok.Data;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultVo<T>  {

    private Byte code;

    private T data;

    private String msg;

    private Long count;

    private static final Byte SUCCESS_STATUS = 0;

    private static final Byte ERROR_STATUS = -1;

    private ResultVo() {
    }

    private ResultVo(Byte code) {
        this.code = code;
    }

    private ResultVo(Byte code, T data) {
        this.code = code;
        this.data = data;
    }

    private ResultVo(Byte code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private ResultVo(Byte code, T data,Long count) {
        this.code = code;
        this.data = data;
        this.count=count;
    }

    public ResultVo createBySuccess() {
        return new ResultVo(SUCCESS_STATUS);
    }

    @JSONField(serialize=false)
    @JsonIgnore
    public boolean isSuccess() {
        return SUCCESS_STATUS.equals(this.code);
    }

    @SuppressWarnings("unchecked")
    public static <T> ResultVo<T> createBySuccessData(T data) {
        return new ResultVo(SUCCESS_STATUS, data);
    }

    @SuppressWarnings("unchecked")
    public static <T> ResultVo<T> createBySuccessData(T data,Long count) {
        return new ResultVo(SUCCESS_STATUS, data,count);
    }



    public static ResultVo createByError(String msg) {
        return new ResultVo(ERROR_STATUS, msg);
    }

    public static ResultVo createByErrorEnum(BusinessErrorEnum resultEnum) {
        return new ResultVo(ERROR_STATUS, resultEnum.getErrMsg());
    }
}
